package com.lqian.demo02.part3;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 *操作数据库的基类
 * @author：Melody_LQ
 * @time：2022/7/13 5:02 下午
 */
public class BaseDao {
    private static final String CONFIG_FILE_PATH = "db.properties";
    private static String driver;
    private static String url;
    private static String username;
    private static String password;
    static{
        init();
    }
    //初始化连接参数，从配置文件中获取
    private static void init() {
        Properties properties = new Properties();

        InputStream is = BaseDao.class.getClassLoader().getResourceAsStream(CONFIG_FILE_PATH);
        try {
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
        driver = properties.getProperty("driver");
        url = properties.getProperty("url");
        username=properties.getProperty("username");
        password=properties.getProperty("password");
    }

    //获取数据库连接
    public static Connection getConnection(){
        Connection connection = null;
        try {
            Class.forName(driver);
            connection = DriverManager.getConnection(url,username,password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 查询操作
     * @param connection 连接对象
     * @param pstm 预处理对象
     * @param rs 结果集
     * @param sql sql语句
     * @param params sql语句的参数
     * @return
     * @throws SQLException
     */
    public static ResultSet execute(Connection connection, PreparedStatement pstm,
                                    ResultSet rs,String sql,Object[] params) throws SQLException {
        pstm = connection.prepareStatement(sql);
        for (int i = 0; i <params.length ; i++) {
            pstm.setObject(i+1,params[i]);
        }
        rs = pstm.executeQuery();
        return rs;
    }

    /**
     * 更新操作
     * @param connection
     * @param pstm
     * @param sql
     * @param params
     * @return  结果影响行 updateRows>1执行成功
     * @throws SQLException
     */
    public static int execute(Connection connection,PreparedStatement pstm,String sql,
                              Object[] params) throws SQLException {
        int updateRows = 0;
        pstm = connection.prepareStatement(sql);
        for (int i = 0; i <params.length ; i++) {
            pstm.setObject(i+1,params[i]);
        }
        updateRows = pstm.executeUpdate();
        return updateRows;
    }

    /**
     * 释放资源
     * @param connection
     * @param pstm
     * @param rs
     * @return
     */
    public static boolean closeResource(Connection connection,PreparedStatement pstm,ResultSet rs){
        boolean flag = true;
        if(rs != null){
            try {
                rs.close();
                rs = null;//GC回收
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                flag = false;
            }
        }
        if(pstm != null){
            try {
                pstm.close();
                pstm = null;//GC回收
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                flag = false;
            }
        }
        if(connection != null){
            try {
                connection.close();
                connection = null;//GC回收
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                flag = false;
            }
        }

        return flag;
    }

    /**
     *  When a Statement object is closed, its current
     *  ResultSet object, if one exists, is also closed.
     * @param connection
     * @param pstm
     * @return
     */
    public static boolean closeResource(Connection connection,PreparedStatement pstm){
        boolean flag = true;
        if(pstm != null){
            try {
                pstm.close();
                pstm = null;//help GC
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }
        if(connection != null){
            try {
                connection.close();
                connection = null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag = false;
            }
        }

        return flag;
    }
}
